package com.example.mywanandroid.ui.home;

import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.mywanandroid.R;
import com.example.mywanandroid.base.BaseFragment;
import com.example.mywanandroid.bean.responsebean.ArticleBean;
import com.example.mywanandroid.bean.responsebean.home.HomeData;
import com.example.mywanandroid.databinding.FragmentListBinding;
import com.example.mywanandroid.navinterface.ScrollToTop;
import com.example.mywanandroid.ui.activity.web.DetailsActivity;
import com.example.mywanandroid.ui.adapter.HomeAdapter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.List;

public class HomeFragment extends BaseFragment<FragmentListBinding, HomeViewModel> implements ScrollToTop {
    private static final String TAG = "HomeFragment";

    public HomeAdapter commonAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void initViewModel() {
        mViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        initDataChange();

    }

    @Override
    protected void bindViewModel() {

    }

    @Override
    protected boolean isSupportLoad() {
        return true;
    }

    @Override
    protected void init() {
        mViewModel.loadHomeData();

        initRecycle();
        initRefreshLayout();
    }

    private void initRecycle() {
        commonAdapter = new HomeAdapter(getActivity());
        mDataBinding.recycle.setLayoutManager(new LinearLayoutManager(getContext()));
        commonAdapter.setOnHomeDataClickListener(new HomeAdapter.OnHomeClickListener() {

            @Override
            public void toDetailsActivity(HomeData itemData) {
                DetailsActivity.start(getActivity(), itemData.getArticleList().getLink());
            }

            @Override
            public void toDetailsActivity(HomeData itemData, int position) {
                String url = itemData.getBannerData().getBannerData().get(position).getUrl();
                DetailsActivity.start(getActivity(), url);
            }

            @Override
            public void onArticleCollectClick(ArticleBean articleList) {
                mViewModel.changeArticleCollect(articleList.isCollect(), articleList.getId());
            }

        });
        commonAdapter.setOnTopArticleClickListener(new HomeAdapter.OnTopArticleClickListener() {
            @Override
            public void onTopCollectClick(ArticleBean articleBean) {
                mViewModel.changeArticleCollect(articleBean.isCollect(), articleBean.getId());
            }

            @Override
            public void onTopClick(ArticleBean articleBean) {
                DetailsActivity.start(getActivity(), articleBean.getLink());
            }
        });
        mDataBinding.recycle.setAdapter(commonAdapter);
    }

    private void initRefreshLayout() {
        mDataBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mViewModel.refreshData(true);
            }
        });
        mDataBinding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mViewModel.refreshData(false);
            }
        });
    }

    @Override
    protected void initCollectState() {
        mViewModel.getCollectStatus().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object object) {
                if (object != null) {
                    commonAdapter.notifyDataSetChanged();
//                    Toast.makeText(getActivity(), "操作成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initDataChange() {
        mViewModel.getHomeList().observe(this, new Observer<List<HomeData>>() {
            @Override
            public void onChanged(List<HomeData> homeData) {
                //TODO:适配器添加数据
                commonAdapter.onItemDatasChanged(homeData);
                mDataBinding.refreshLayout.finishRefresh();
                mDataBinding.refreshLayout.finishLoadMore();
            }
        });
    }

    public void reFreshList() {
        Log.e(TAG, "homeFragment 重新获取数据 ");
        mViewModel.loadHomeData();
    }

    @Override
    public void scrollToTop() {
        mDataBinding.recycle.smoothScrollToPosition(0);
    }
}
